/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kma.summer2012.dao;

import com.kma.summer2012.factory.DAOFactory;
import com.kma.summer2012.factory.HibernateUtil;
import com.kma.summer2012.factory.SpringBeansFactory;
import com.kma.summer2012.model.CrumbsUser;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Restrictions;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * @author Администратор
 */
public class CrumbsUserDAOImpl implements CrumbsUserDAO/*DAO<CrumbsUser>*/ {


    public void add(CrumbsUser user) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.save(user);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }


    public void update(CrumbsUser user) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.update(user);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }


    public CrumbsUser getById(int user_id) {
        Session session = null;
        CrumbsUser user = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            user = (CrumbsUser) session.get(CrumbsUser.class, user_id);
            if (user.getCrumbsRequests() != null) Hibernate.initialize(user.getCrumbsRequests());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return user;
    }

    public CrumbsUser getByPositionId(int positionID) {
        DAOFactory factory = SpringBeansFactory.getInstance().getBeanByClass(DAOFactory.class);

        Session session = null;
        CrumbsUser user = null;

        try {
            session = HibernateUtil.getSessionFactory().openSession();
            user = (CrumbsUser) session.createCriteria(CrumbsUser.class).add(Restrictions.eq("crumbsPosition", factory.getCrumbsPositionDAO().getById(positionID))).uniqueResult();
            Hibernate.initialize(user.getCrumbsRequests());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return user;
    }

    public Collection<CrumbsUser> getAll() {
        Session session = null;
        List users = new ArrayList<CrumbsUser>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            users = session.createCriteria(CrumbsUser.class).list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return users;
    }


    public void delete(CrumbsUser user) {
        Session session = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            session.beginTransaction();
            session.delete(user);
            session.getTransaction().commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
    }

    public CrumbsUser searchByLoginPassword(String login, String password) {
        Session session = null;
        CrumbsUser user = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            user = (CrumbsUser) session.createCriteria(CrumbsUser.class).add(Expression.eq("login", login)).add(Expression.eq("password", password)).uniqueResult();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return user;
    }

    @Override
    public CrumbsUser searchByLogin(String login) throws SQLException {
        Session session = null;
        CrumbsUser user = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            user = (CrumbsUser) session.createCriteria(CrumbsUser.class).add(Expression.eq("login", login)).uniqueResult();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return user;
    }

    public Collection<CrumbsUser> getAllEngineers() {
        DAOFactory factory = SpringBeansFactory.getInstance().getBeanByClass(DAOFactory.class);
        Session session = null;
        List users = new ArrayList<CrumbsUser>();
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            users = session.createCriteria(CrumbsUser.class).setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).add(Restrictions.eq("crumbsPosition", factory.getCrumbsPositionDAO().getById(3))).list();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (session != null && session.isOpen()) {
                session.close();
            }
        }
        return users;
    }


}
